
package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.activity.ActivitysDetailActivity;
import com.xiaoma.myaudience.biz.model.ActivityDetailInfoModel;
import com.xiaoma.myaudience.biz.model.RankingListModel;
import com.xiaoma.myaudience.biz.task.AddEventTask;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

/**
 * 活动筛选Adapter
 * @author jpren
 *
 */
public class ActivitySelectAdapter extends BaseAdapter implements OnClickListener{
    private List<Map<String, Object>> mList;	
    private Context mContext;
    private LayoutInflater mInflater;
    private String mSizeStr;
	private AddEventTask mAddEventTask;
	private String mErrorMsg;
    
    public ActivitySelectAdapter(Context context, List<Map<String, Object>> list) {
        mList = list;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.list_item_img_width), (int) context.getResources().getDimension(
                R.dimen.list_item_img_height));
    }

    @Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.activitys_select_item, null);
			ViewHolder holder = new ViewHolder();
			holder.img = (LoadingImageView) convertView.findViewById(R.id.img);
			holder.score = ((TextView)convertView.findViewById(R.id.score));
			holder.ratingBar = ((RatingBar)convertView.findViewById(R.id.rating_bar));
			holder.title = ((TextView)convertView.findViewById(R.id.title));
			holder.zcr = ((TextView)convertView.findViewById(R.id.zcr));
			holder.type = ((TextView)convertView.findViewById(R.id.type));
			holder.jb = ((TextView)convertView.findViewById(R.id.jb));
			holder.location = ((TextView)convertView.findViewById(R.id.location));
			holder.startTime = ((TextView)convertView.findViewById(R.id.start_time));
			holder.datelineTime = ((TextView)convertView.findViewById(R.id.dateline_time));
			holder.limitNum = ((TextView)convertView.findViewById(R.id.limit_num));
			holder.verify = ((TextView)convertView.findViewById(R.id.verify));
			holder.jifeng = ((TextView)convertView.findViewById(R.id.jifeng));
			holder.allTime = ((TextView)convertView.findViewById(R.id.all_time));
			holder.play = ((TextView)convertView.findViewById(R.id.play));
			
            convertView.setTag(holder);
		}
		ViewHolder holder = (ViewHolder) convertView.getTag();
		final Map<String, Object> item = (Map<String, Object>) getItem(position);
		//get eventid
		final String eventid = ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_EVENTID);
		holder.img.setLoadingDrawable(ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_POSTER), mSizeStr);
		String score = ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_STARLEVEL);
		holder.score.setText(score);
		Double d = Math.ceil(Double.valueOf(score)/2.0);
		holder.ratingBar.setRating(Float.valueOf(d.toString()));
		String title = ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_TITLE);
		holder.title.setText(title);
		holder.zcr.setText("主持人：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_ZCR));
		holder.type.setText("活动类型：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_CLASSNAME));
		holder.jb.setText("活动嘉宾：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_JB));
		holder.location.setText("活动地点：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_LOCATION));
		holder.startTime.setText("开始时间：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_STARTTIME));
		holder.datelineTime.setText("截止报名：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_DEADTIME));
		holder.limitNum.setText("活动人数：" + ModelUtils.getStringValue(item, RankingListModel.PARAM_LIMITNUM));
		holder.verify.setText("需要审核：" + ("1".equals(ModelUtils.getStringValue(item, RankingListModel.PARAM_VERIFY)) ? "需要" : "不需要"));
		String jifen =  ModelUtils.getStringValue(item, RankingListModel.PARAM_JIFENG) ;
		int sjififen = Integer.valueOf(jifen);
		String jfString = (sjififen < 0 ? "参加活动：需要花费 " + (-sjififen) : "参加活动：奖励" + sjififen) + "能量/人";
		holder.jifeng.setText(jfString);
		holder.allTime.setText(String.format("%s次查看 | %s人参加 | %s待审核", ModelUtils.getStringValue(item, RankingListModel.PARAM_VIEWNUM) ,
				ModelUtils.getStringValue(item, RankingListModel.PARAM_MEMBERNUM), 
				ModelUtils.getStringValue(item, RankingListModel.PARAM_SHENHE)));
		String mStatus = ModelUtils.getStringValue(item, ActivityDetailInfoModel.PARAM_STATUS);
		getStatus(Integer.valueOf(mStatus), holder.play);
		
		holder.img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				eventid
				Intent intent = new Intent(mContext, ActivitysDetailActivity.class);
				intent.putExtra(ActivityDetailInfoModel.PARAM_EVENTID, eventid);
				mContext.startActivity(intent);
			}
		});
		
		return convertView;
	}
	
	private void getStatus(int status, TextView btn) {
		String name = "参加活动";
		switch (status) {
		case 0:
			name = "参加活动";
			btn.setBackgroundResource(R.drawable.add_event_selector);
			break;
		case 1:
			name = "报名已截止";
			btn.setEnabled(false);
			btn.setBackgroundResource(R.drawable.rank_look_selector);
			break;
		case 2:
			name = "活动已结束";
			btn.setEnabled(false);
			btn.setBackgroundResource(R.drawable.rank_look_selector);
			
			break;
		case 3:
			name = "退出活动（待审核）";
			btn.setBackgroundResource(R.drawable.add_event_selector);
			break;
		case 4:
			name = "退出活动（已审核）";
			btn.setBackgroundResource(R.drawable.add_event_selector);
			break;

		default:
			break;
		}
		
		btn.setText(name);
	}
	
	private class ViewHolder {
        LoadingImageView img;
        TextView score;
        RatingBar ratingBar;
        TextView title;
        TextView zcr;
        TextView type;
        TextView jb;
        TextView location;
        TextView startTime;
        TextView datelineTime;
        TextView limitNum;
        TextView verify;
        TextView jifeng;
        TextView allTime;
        TextView play;
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img:
			
			break;

		default:
			break;
		}
	}
}